package util
{
	import flash.events.Event;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.FileReferenceList;
	import flash.net.URLRequest;

	public class FileUpload
	{
		import mx.controls.Alert;
		private var myFileRefrence:FileReferenceList;//文件调控器
		
		public function FileUpload()
		{
		}
		
		public function upload():void {
			var imagesFilter:FileFilter = new FileFilter("Images","*.jpg;*.gif;*.png");
//			var docFilter:FileFilter = new FileFilter("Documents","*.pdf;*.doc;*.txt");
			myFileRefrence = new FileReferenceList();
			myFileRefrence.addEventListener(Event.SELECT,selectHandler);//选择完后的处理
			myFileRefrence.browse([imagesFilter]);//显示文件浏览对话框，用户选择一个或多个药上传的文件
		}
		/**选择完上传文件的处理方法**/  
		private function selectHandler(event:Event):void {
			//得到要上传的文件
			var fileArray:Array = myFileRefrence.fileList as Array;
			var requestUrl:URLRequest = new URLRequest("FileUploadServlet");
			var oneReference:FileReference =  fileArray.pop() as FileReference; //上传了 docFilter 剩下imagesFilter
			oneReference.addEventListener(Event.COMPLETE,uploadIterative);//监听迭代上传方法  
			oneReference.upload(requestUrl);//上传到java中的该方法FileUploadServlet
		}
		//迭代上传
		private function uploadIterative(event:Event):void {
			Alert.show((event.target as FileReference).name+"上传完成！")   
			if(myFileRefrence.fileList.length>0) {
				selectHandler(null);
			}
		}
		
	}
}